<template>
  <a :href="url" ref="downloaLink" :download="fileName"> </a>
</template>

<script>
export default {
  name: 'QDownload',
  props: {
    hrefUrl: {
      type: String,
      default: ''
    },
    fileName: {
      type: String,
      default: ''
    },
    data: {
      type: Object,
      default: null
    }
  },
  computed: {
    url () {
      let url = this.hrefUrl
      const downData = this.data
      if (downData) {
        const params = Object.keys(downData).map(function (key) {
          const value = downData[key]
          return typeof (value) !== 'undefined' && value !== null ? encodeURI(key) + '=' + encodeURI(value) : ''
        }).join('&')
        url += '?' + params
      }
      return url
    }
  },
  methods: {
    download () {
      this.$nextTick(() => {
        this.$refs.downloaLink.click()
      })
    }
  }
}
</script>
